<?php

/**
 * 案件记录
 */
class CaserecordController extends Controller_Base
{


    //案件管理列表
    public function indexAction()
    {
        $submit_type = $this->getRequest()->getQuery('submit', 0);
        if ($submit_type == 3) {
            $this->redirect('/caserecord/index');
            exit;
        }

        $tj = 0;
        if (count($_GET)>1) {
            $tj = 1;
        }
        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $case_id = $request->getQuery('case_id', '');
        $caseStatus = $request->getQuery('caseStatus', 0);
        $limit = intval($request->getQuery('limit', 10));
        $litigantName = trim($request->getQuery('litigantName', ''));
        $mediatorId = intval($request->getQuery('mediatorId', 0));
        $is_intents = intval($request->getQuery('is_intents', 0));
        $is_show = intval($request->getQuery('is_show', 0));
        $batshId = trim($request->getQuery('batshId', ''));
        $mediationOrganId = ($request->getQuery('mediationOrganId', 0));
        $mediateStatus = ($request->getQuery('mediateStatus', 0));
        $assetsId = $request->getQuery('assetsId', 0);
        $ydRepairStatus = $request->getQuery('ydRepairStatus', 0);
        $ltRepairStatus = $request->getQuery('ltRepairStatus', 0);
        $sysStatus = $request->getQuery('sysStatus', 1);
        $customGrade = $request->getQuery('customGrade', []);
        $customLabel = $request->getQuery('customLabel', []);
        $importRecordId = $request->getQuery('importRecordId', 0);
        $batchName = $request->getQuery('batchName', '');
        $contractNo = $request->getQuery('contractNo', '');
        $caseType = $request->getQuery('caseType', 0);
        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;
        $mediate_expire_time = intval($request->getQuery('mediate_expire_time', 0));
        $caseSource = ($request->getQuery('caseSource', 0));
        $where = $this->getWhere();

        $query = array('limit' => $limit);

        if ($litigantName) {//被告姓名
            $case_ids = $this->getCaseIdByLitigantName($litigantName);
            array_push($where, "id in ({$case_ids})");
        }

        if ($case_id) {//按照案件号查询
            array_push($where, "id = '{$case_id}'");
        }

        if ($mediatorId) {//按照案件号查询
            array_push($where, "mediatorId = '{$mediatorId}'");
        }

        if ($caseStatus) {//分配状态
            array_push($where, "caseStatus = '{$caseStatus}'");
        }

        if ($batshId) {//分配状态
            array_push($where, "batshId = '{$batshId}'");
        }
        if ($caseSource) {
            array_push($where, "caseSource = {$caseSource}");
            $query['caseSource'] = $caseSource;
        }
        if ($ydRepairStatus) {
            array_push($where, "ydRepairStatus = {$ydRepairStatus}");
            $query['ydRepairStatus'] = $ydRepairStatus;
        }
        if ($ltRepairStatus) {
            array_push($where, "ltRepairStatus = {$ltRepairStatus}");
            $query['ltRepairStatus'] = $ltRepairStatus;
        }
        if ($sysStatus) {
            array_push($where, "sysStatus = {$sysStatus}");
            $query['sysStatus'] = $sysStatus;
        }

        if ($customGrade) {
            $temp = implode(",", $customGrade);
            array_push($where, "customGrade in ({$temp})");
            $query['customGrade'] = $customGrade;
        }

        if ($importRecordId) {
            array_push($where, "importRecordId in ({$importRecordId})");
            $query['importRecordId'] = $importRecordId;
        }
        if ($customLabel) {
            $temp = implode(",", $customLabel);
            array_push($where, "customLabel in ({$temp})");

            $query['customLabel'] = $customLabel;
        }

        if ($mediateStatus) {
            array_push($where, "mediateStatus = '{$mediateStatus}'");
            $query['mediateStatus'] = $mediateStatus;
        }
        if ($batchName) {
            array_push($where, "batchName = '{$batchName}'");
            $query['batchName'] = $batchName;
        }
        if ($contractNo) {
            array_push($where, "contractNo = '{$contractNo}'");
            $query['contractNo'] = $contractNo;
        }
        if ($caseType) {
            array_push($where, "caseType = {$caseType}");
            $query['caseType'] = $caseType;
        }

        if ($mediate_expire_time) {
            if ($mediate_expire_time == 999) {
                $now = time();
                array_push($where, "entrustExpireTime < '{$now}' AND entrustExpireTime> 0");
            } else {
                $start_time = strtotime(date("Y-m-d", time()));
                $end_time = $start_time + ($mediate_expire_time * 86400);
                array_push($where, "entrustExpireTime > '{$start_time}' AND entrustExpireTime < '{$end_time}'");
            }
            $query['mediate_expire_time'] = $mediate_expire_time;

        }

        if ($assetsId) {
            array_push($where, "assetsId = {$assetsId}");
            $query['assetsId'] = $assetsId;
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if($add_begin_end){
            $query['add_begin_end'] = $add_begin_end;
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "addTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "addTime >= {$begin_date}");
            }
        }


        if ($is_intents) {
            array_push($where, "intents = {$is_intents}");
        }

        $query['mediationOrganId'] = $mediationOrganId;
        $query['is_intents'] = $is_intents;
        $query['mediatorId'] = $mediatorId;
        $query['litigantName'] = $litigantName;
        $query['caseStatus'] = $caseStatus;
        $query['case_id'] = $case_id;
        $query['batshId'] = $batshId;

        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => $start,
            'order' => 'id DESC'
        );
        $cases = Module_Mediate_Case::getInstance()->getAllBySearch(array("*"), $seacher, true);

        if($tj){
            $count = Module_Mediate_Case::getInstance()->getAllBySearch(array("count(*) as num","sum(debtTotal) as debtTotal","sum(debtAmount) as debtAmount",'COUNT(DISTINCT cardId) as groupTotal'), array('where' => $where), true);
        }else{
            $count = Module_Mediate_Case::getInstance()->getAllBySearch(array("count(*) as num"), array('where' => $where), true);
        }
        $total = $count[0]['num'];
        $totalPage = ceil($total / $limit);

        $this->getMediator();

        $this->getMediationOrgan();
        //赋值资产方
        $this ->getAssets();
        SmartyAdapter::assign('mediateStatus', Module_Mediate_Case::$mediateStatus);
        SmartyAdapter::assign('caseStatusArr', Module_Mediate_Case::$caseStatusArr);
        SmartyAdapter::assign('sysStatus', Module_Mediate_Case::$sysStatusArr);
        SmartyAdapter::assign('customLabel', Module_Mediate_Case::$customLabel);
        SmartyAdapter::assign('customGrade', Module_Mediate_Case::$customGrade);
        SmartyAdapter::assign('disputeType',  Module_Mediate_Case::$disputeType);
        SmartyAdapter::assign('arrList', $cases);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('is_show', $is_show);
        SmartyAdapter::assign('debtTotal', empty($count[0]['debtTotal']) ? 0 : $count[0]['debtTotal']);
        SmartyAdapter::assign('debtAmount',  empty($count[0]['debtAmount']) ? 0 : $count[0]['debtAmount']);
        SmartyAdapter::assign('groupTotal', empty($count[0]['groupTotal']) ? 0 : $count[0]['groupTotal']);        //$this->getPlaintiff();
        SmartyAdapter::assign('tj', $tj);        //$this->getPlaintiff();
        SmartyAdapter::assign('page_query', http_build_query($query));
    }


    //案件导出
    public function exportinfoAction()
    {
        $request = $this->getRequest();
        $case_id = $request->getQuery('case_id', '');
        $caseStatus = $request->getQuery('caseStatus', 0);
        $litigantName = trim($request->getQuery('litigantName', ''));
        $mediatorId = intval($request->getQuery('mediatorId', 0));
        $is_intents = intval($request->getQuery('is_intents', 0));
        $batshId = trim($request->getQuery('batshId', ''));
        $mediateStatus = ($request->getQuery('mediateStatus', 0));
        $assetsId = $request->getQuery('assetsId', 0);
        $ydRepairStatus = $request->getQuery('ydRepairStatus', 0);
        $ltRepairStatus = $request->getQuery('ltRepairStatus', 0);
        $sysStatus = $request->getQuery('sysStatus', 1);
        $customGrade = $request->getQuery('customGrade', []);
        $customLabel = $request->getQuery('customLabel', []);
        $importRecordId = $request->getQuery('importRecordId', 0);
        $batchName = $request->getQuery('batchName', '');
        $contractNo = $request->getQuery('contractNo', '');
        $mediate_expire_time = intval($request->getQuery('mediate_expire_time', 0));
        $caseSource = ($request->getQuery('caseSource', 0));
        $caseType = $request->getQuery('caseType', '');

        $where = $this->getWhere();

        if ($litigantName) {//被告姓名
            $case_ids = $this->getCaseIdByLitigantName($litigantName);
            array_push($where, "id in ({$case_ids})");
        }

        if ($case_id) {//按照案件号查询
            array_push($where, "id = '{$case_id}'");
        }

        if ($mediatorId) {//按照案件号查询
            array_push($where, "mediatorId = '{$mediatorId}'");
        }
        if ($batchName) {
            array_push($where, "batchName = '{$batchName}'");
        }
        if ($contractNo) {
            array_push($where, "contractNo = '{$contractNo}'");
        }
        if ($caseType) {
            array_push($where, "caseType = {$caseType}");
        }

        if ($caseStatus) {//分配状态
            array_push($where, "caseStatus = '{$caseStatus}'");
        }

        if ($batshId) {//分配状态
            array_push($where, "batshId = '{$batshId}'");
        }

        if ($caseSource) {
            array_push($where, "caseSource = {$caseSource}");
            $query['caseSource'] = $caseSource;
        }
        if ($ydRepairStatus) {
            array_push($where, "ydRepairStatus = {$ydRepairStatus}");
            $query['ydRepairStatus'] = $ydRepairStatus;
        }
        if ($ltRepairStatus) {
            array_push($where, "ltRepairStatus = {$ltRepairStatus}");
            $query['ltRepairStatus'] = $ltRepairStatus;
        }
        if ($sysStatus) {
            array_push($where, "sysStatus = {$sysStatus}");
            $query['sysStatus'] = $sysStatus;
        }

        if ($customGrade) {
            $temp = implode(",", $customGrade);
            array_push($where, "customGrade in ({$temp})");
            $query['customGrade'] = $customGrade;
        }

        if ($importRecordId) {
            array_push($where, "importRecordId = {$importRecordId}");
            $query['importRecordId'] = $importRecordId;
        }
        if ($customLabel) {
            $temp = implode(",", $customLabel);
            array_push($where, "customLabel in ({$temp})");

            $query['customLabel'] = $customLabel;
        }


        if ($mediateStatus) {
            array_push($where, "mediateStatus = '{$mediateStatus}'");
            $query['mediateStatus'] = $mediateStatus;
        }

        if ($mediate_expire_time) {
            if ($mediate_expire_time == 999) {
                $now = time();
                array_push($where, "entrustExpireTime < '{$now}' AND entrustExpireTime> 0");
            } else {
                $start_time = strtotime(date("Y-m-d", time()));
                $end_time = $start_time + ($mediate_expire_time * 86400);
                array_push($where, "entrustExpireTime > '{$start_time}' AND entrustExpireTime < '{$end_time}'");
            }
            $query['mediate_expire_time'] = $mediate_expire_time;

        }

        if ($assetsId) {
            array_push($where, "assetsId = {$assetsId}");
            $query['assetsId'] = $assetsId;
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if($add_begin_end){
            $query['add_begin_end'] = $add_begin_end;
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "addTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "addTime >= {$begin_date}");
            }
        }
        if ($is_intents) {
            array_push($where, "intents = {$is_intents}");
        }

        $seacher = array(
            'where' => $where,
            'order' => 'id'
        );
        $list = Module_Mediate_Case::getInstance()->getAllBySearch(array("*"), $seacher, true);

        $down = [];
        if (!empty($list)) {
            $this->getMediationOrgan();
            $mediationOrgan= SmartyAdapter::getTplVars('mediationOrgan');
            $mediationOrgan= array_column($mediationOrgan,null,'id');
            $this->getAssets();
            $getAssets= SmartyAdapter::getTplVars('assets');
            $getAssets= array_column($getAssets,null,'id');

            $caseStatusArr = Module_Mediate_Case::$caseStatusArr;
            //$mediateStatus= Module_Mediate_Case::$mediateStatus;
            $repairStatus = [9=>"未修复",1=>"修复成功",2=>"修复失败"];

            foreach ($list as $k => $_info) {
                $down[$k]['mediationNum'] = $_info['mediationNum'];
                $down[$k]['mediationOrgan'] = $mediationOrgan[$_info['mediationOrganId']]['name'];
                $down[$k]['assetsId'] = $getAssets[$_info['assetsId']]['name'];
                $down[$k]['litigantName'] = $_info['litigantName'];
                $down[$k]['customLabel'] = Module_Mediate_Case::$customLabel[$_info['customLabel']];
                $down[$k]['customGrade'] = Module_Mediate_Case::$customGrade[$_info['customGrade']];
                $down[$k]['mediatorName'] = $_info['mediatorName'];
                $down[$k]['caseStatus'] = $caseStatusArr[$_info['caseStatus']];
                $down[$k]['mediateResult'] = Module_Mediate_Case::$mediateResult[$_info['mediateResult']];
                $down[$k]['ydRepairStatus'] = $repairStatus[$_info['ydRepairStatus']];
                $down[$k]['ltRepairStatus'] = $repairStatus[$_info['ltRepairStatus']];
                $down[$k]['repayStatus'] = Module_Mediate_Case::$repayStatusArr[$_info['repayStatus']];
                $down[$k]['batshId'] = $_info['batshId'];
                $down[$k]['updateTime'] = date("Y-m-d H:i:s", $_info['updateTime']);
                $down[$k]['entrustExpireTime'] = date("Y-m-d H:i:s",$_info['entrustExpireTime']);
                $down[$k]['contractNo'] = $_info['contractNo'];
                $down[$k]['debtAmount'] = $_info['debtAmount'];
                $down[$k]['debtTotal'] = $_info['debtTotal'];
            }
        }
        $header = ['调解案号', '调解部门','资产方', '当事人','用户标签','用户等级','调解员','案件状态','调解结果','移动修复','联通修复','还款状态','案件标识号','最近修改时间','案件到期时间','合同号','欠款本金','欠款总额'];
        Lib_XlsxExport::getInstance()->downExcel($down,$header,'案件记录' );
        exit;
    }


}
